import {
  Controller,
  Get,
  Param,
  Query,
  BadRequestException,
} from '@nestjs/common';
import { ChatroomService } from './chatroom.service';
import { RequireLogin, UserInfo } from 'src/custom.decorator';

@Controller('chatroom')
@RequireLogin()
export class ChatroomController {
  constructor(private readonly chatroomService: ChatroomService) {}

  // 创建私聊
  @Get('create-private')
  async createOneToOneChatroom(
    @UserInfo('userId') userId: number,
    @Query('friendId') friendId: number,
  ) {
    if (!friendId) {
      throw new BadRequestException('请选择要聊天的好友');
    }
    return await this.chatroomService.createOneToOneChatroom(userId, friendId);
  }

  // 创建群聊
  @Get('create-group')
  async createGroupChatroom(
    @UserInfo('userId') userId: number,
    @Query('name') name: string,
    @Query('avatar') avatar: string,
    @Query('friendIds') friendIds: string,
  ) {
    if (!name) {
      throw new BadRequestException('请输入群聊名称');
    }
    if (!friendIds) {
      throw new BadRequestException('请选择群聊成员');
    }
    const memberIdsArr = JSON.parse(friendIds);
    if (memberIdsArr.length) {
      return await this.chatroomService.createGroupChatroom(
        userId,
        name,
        avatar,
        memberIdsArr,
      );
    } else {
      throw new BadRequestException('friendIds 数据格式不正确');
    }
  }

  // 获取房间列表
  @Get('list')
  async list(@UserInfo('userId') userId: number, @Query('name') name: string) {
    if (!userId) {
      throw new BadRequestException('请先登录');
    }
    return await this.chatroomService.list(userId, name);
  }

  // 获取房间成员
  @Get('members')
  async members(@Query('roomId') roomId: number) {
    if (!roomId) {
      throw new BadRequestException('roomId 不能为空');
    }
    return await this.chatroomService.members(roomId);
  }

  // 获取房间信息
  @Get('info/:roomId')
  async info(@Param('roomId') roomId: number) {
    if (!roomId) {
      throw new BadRequestException('roomId 不能为空');
    }
    return await this.chatroomService.info(roomId);
  }

  // 查找用户们是否已在一个群聊中
  @Get('find')
  async find(
    @UserInfo('userId') userId: number,
    @Query('memberIds') memberIds: string,
  ) {
    if (!userId) {
      throw new BadRequestException('userId 不能为空');
    }
    if (!memberIds) {
      throw new BadRequestException('memberIds 不能为空');
    }
    const userIdsArr = JSON.parse(memberIds);
    if (userIdsArr.length) {
      return await this.chatroomService.find(userId, userIdsArr);
    } else {
      throw new BadRequestException('memberIds 数据格式不正确');
    }
  }

  // 把用户拉入房间
  @Get('join/:roomId')
  async join(
    @Param('roomId') roomId: number,
    @Query('userIds') userIds: string,
  ) {
    if (!roomId) {
      throw new BadRequestException('roomId 不能为空');
    }
    if (!userIds) {
      throw new BadRequestException('userIds 不能为空');
    }

    const userIdsArr = JSON.parse(userIds);
    if (userIdsArr.length) {
      return await this.chatroomService.join(roomId, userIdsArr);
    } else {
      throw new BadRequestException('memberIds 数据格式不正确');
    }
  }

  // 把用户踢出房间
  @Get('quit/:roomId')
  async quit(
    @Param('roomId') roomId: number,
    @Query('userIds') userIds: string,
  ) {
    if (!roomId) {
      throw new BadRequestException('roomId 不能为空');
    }
    if (!userIds) {
      throw new BadRequestException('userIds 不能为空');
    }
    const userIdsArr = JSON.parse(userIds);
    if (userIdsArr.length) {
      return await this.chatroomService.quit(roomId, userIdsArr);
    } else {
      throw new BadRequestException('memberIds 数据格式不正确');
    }
  }

  // 销毁房间
  @Get('destroy/:roomId')
  async destroy(@Param('roomId') roomId: number) {
    if (!roomId) {
      throw new BadRequestException('roomId 不能为空');
    }
    return await this.chatroomService.destroy(roomId);
  }

  // 获取未读消息
  @Get('unread')
  async unread(@UserInfo('userId') userId: number) {
    if (!userId) {
      throw new BadRequestException('userId 不能为空');
    }
    return await this.chatroomService.getUnread(userId);
  }

  @Get('clearUnRead/:id')
  async clearUnread(
    @UserInfo('userId') userId: number,
    @Param('id') roomId: number,
  ) {
    if (!userId) {
      throw new BadRequestException('userId 不能为空');
    }
    if (!roomId) {
      throw new BadRequestException('roomId 不能为空');
    }
    return await this.chatroomService.clearUnread(userId, roomId);
  }
}
